package com.ht.searchrooky.client.gui.service;

import javax.jms.Connection;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.TopicSession;

import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.Context;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;


public class ReceiveInfListener extends Thread implements MessageListener
{
    Context ctx;
    Connection conn;
    Session session;
    JTextPane label;
  
    
    public ReceiveInfListener(JTextPane label)
    {
	super();
	this.label=label;
	
    }



    public void run (){
	
		try
		{
//		    ctx = new javax.naming.InitialContext();
//		
//			javax.jms.ConnectionFactory factory = (javax.jms.ConnectionFactory)ctx.lookup("ConnectionFactory");
//	
//			 conn = factory.createConnection();
//			session =conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//			Queue topic=(Queue)ctx.lookup("MyQueue");
//			
//
//			MessageConsumer mc=session.createConsumer(topic);
//			
//			System.out.println(topic);
			
			
			javax.naming.Context ctx = new javax.naming.InitialContext();
		
			javax.jms.TopicConnectionFactory factory = (javax.jms.TopicConnectionFactory)ctx.lookup("ConnectionFactory");
		
			javax.jms.TopicConnection conn = factory.createTopicConnection();
			TopicSession session =conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic top=(Topic)ctx.lookup("InformationTopic");
			

		MessageConsumer mc=session.createConsumer(top);
			conn.start();
		mc.setMessageListener(this);
		}
		catch (Exception e)
		{
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		
	
    }
    
    
    public static void main(String[] args)
    {
	//new ReceiveInfListener(new JLabel()).start();
	//new ReceiveInfListener().start();
    }


	@Override
	public void onMessage(Message arg0) {
	System.out.println(arg0);
	
	try {
		label.setText(((TextMessage)arg0).getText());
	} catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	label.show(true);
		
	}
}
